//---------------------------------------------------------------------
// File: EntLibDatabaseDeleteStep.cs
// 
// Summary: 
//---------------------------------------------------------------------


using System.Xml;
using BizUnit.Extensions.EntLib.Utilities;
using Microsoft.Services.BizTalkApplicationFramework.BizUnit;

namespace BizUnit.Extensions
{
    /// <summary>
    /// The EntLibDatabaseDeleteStep step uses the supplied connection string, table name and condition to delete rows from the database
    /// </summary>
    /// 
    /// <remarks>
    /// The following shows an example of the Xml representation of this test step.
    /// 
    /// <code escaped="true">
    ///	<TestStep assemblyPath="" typeName="Microsoft.Services.BizTalkApplicationFramework.BizUnit.EntLibDatabaseDeleteStep">
    ///		<ConfigFileLocation>..\..\db.config</ConfigFileLocation>
    ///     <DatabaseConfigName>Northwind</DatabaseConfigName>
    ///		<Table>table</Table>
    ///		<Condition>id=1</Condition>
    ///	</TestStep>
    ///	</code>
    ///	
    ///	<list type="table">
    ///		<listheader>
    ///			<term>Tag</term>
    ///			<description>Description</description>
    ///		</listheader>
    ///		<item>
    ///			<term>ConfigFileLocation</term>
    ///			<description>Location of the Database configuration file</description>
    ///		</item>
    ///     <item>
    ///			<term>DatabaseConfigName</term>
    ///			<description>Name of the Database to be used in the configuration file</description>
    ///		</item>
    ///		<item>
    ///			<term>Table</term>
    ///			<description>Table name to delete rows from</description>
    ///		</item>
    ///		<item>
    ///			<term>Condition</term>
    ///			<description>Condition to use</description>
    ///		</item>
    ///	</list>
    ///	</remarks>

    public class EntLibDatabaseDeleteStep : ITestStep
    {
		/// <summary>
		/// ITestStep.Execute() implementation
		/// </summary>
		/// <param name='testConfig'>The Xml fragment containing the configuration for this test step</param>
		/// <param name='context'>The context for the test, this holds state that is passed beteen tests</param>
		public void Execute(XmlNode testConfig, Context context)
        {
            string configFileLocation = context.ReadConfigAsString(testConfig, "ConfigFileLocation");
            string databaseConfigName = context.ReadConfigAsString(testConfig, "DatabaseConfigName");
            string table = context.ReadConfigAsString(testConfig, "Table");
            string condition = context.ReadConfigAsString(testConfig, "Condition" );

            // Build the SQL statement
            string sqlStatement = "delete from " + table + " where " + condition ;

            context.LogInfo( "EntLibDatabaseDeleteStep connecting to database configured in \"{0}\", executing statement \"{1}\"", configFileLocation, sqlStatement ) ;

            // Execute command against specified database
            EntLibDatabaseHelper.ExecuteNonQuery(configFileLocation, databaseConfigName, sqlStatement ) ;
        }
    }
}
